home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL09.TXT < prev    next >
Text File  |  1986-04-05  |  3KB  |  112 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 36
  2.  
  3.  
  4. TURBO-LESSON 9: FOR STATEMENT
  5.  
  6. OBJECTIVES - In lesson 9, you will learn about:
  7.  
  8. 1.  Using ":n" to specify field width in Write statements
  9. 2.  FOR statement
  10.  
  11.  
  12. 1.  Using ":n" to specify field width in Write statements.
  13.  
  14. Spacing of output in previous lessons has been done using spaces 
  15. in string constants, such as, 'I= '.  The space after the = will 
  16. be printed as part of the string.
  17.  
  18. There is another way to add spaces.  When listing the items to 
  19. write in a Write or WriteLn statement, ":n" can be added to 
  20. specify how many characters the item is to occupy in the output.
  21.  
  22. For example:
  23.  
  24. WriteLn(I:4);
  25.  
  26. The value of I would be printed in a space 4 characters wide.
  27.  
  28. ##### DO:
  29.  
  30. Run Program TEST1 several times with the following statements in 
  31. the main part of the program: 
  32.  
  33. ReadLn(I);
  34. WriteLn('[', I:4, ']');
  35.  
  36. Look closely at the output.  Are the numbers "left-justified" or 
  37. "right-justified"?  
  38.  
  39. If I has a value of 23, and 23 prints in columns 1 and 2 of the 
  40. 4 column field, it is left-justified.  If the 23 appears in 
  41. columns 3 and 4 of the 4 colunm field, it is right-justified.  
  42.  
  43. What happens if you enter a number that is too large to fit the 
  44. field specified?
  45.  
  46. ##### DO:
  47.  
  48. Run the program several times using the following values for I:
  49.  
  50. 123,   1234,   -1234,   12345
  51. î
  52. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 37 
  53.  
  54.  
  55. 2.  FOR statement.
  56.  
  57. In the previous lesson, you were reminded of the various ways to 
  58. control the sequencing of program actions.  One of the REPETITION
  59. structures is the FOR statement.  The forms of the statement are:
  60.  
  61. FOR variable := lowvalue TO highvalue DO
  62.      Statement;   {May be a block statement with BEGIN END}
  63.  
  64. FOR variable := highvalue DOWNTO lowvalue DO
  65.      Statement;
  66.  
  67. ##### DO:
  68.  
  69. Run PROG9 several times to see how it works.
  70.  
  71. Experiment with both positive and negative numbers.
  72.  
  73. What happens if High is smaller than Low?
  74.  
  75. ##### DO:
  76.  
  77. Run PROG9 using 5 for the Low, 3 for the High?
  78.  
  79. What was the result?
  80.  
  81. Also try 5 for both values.  How many times is the loop repeated 
  82. if Low = High?
  83.  
  84. What results would you expect if you entered 3.4 for Low and 5.3 
  85. for High?
  86.  
  87. ##### DO:
  88.  
  89. Run the program with the values 3.4 and 5.3.
  90.  
  91. Were you correct in predicting the outcome?
  92.  
  93. Can constants be used instead the variables Low and High?
  94.  
  95. ##### DO:
  96.  
  97. Edit the FOR statement in PROG9 as follows:
  98.  
  99. FOR Index := 1 to 5 DO
  100.  
  101. Run the Program.  
  102.  
  103. Is it possible to use expressions for Low and High?
  104.  
  105. ##### DO:
  106.  
  107. Change the FOR statement to:
  108.  
  109. FOR Index := Low + 1 TO High DO
  110.  
  111. Run the Program.  
  112. î